using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectGopY()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOPY
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectGopYPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOPY,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListGopY", CreateViewModel(data));
		}


		public ActionResult DeleteGopY(int id, int page = 1)
		{
			var result = _repGopY.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_GOPY,
				CurrentPage = page
			};
			return View("SelectGopY", CreateViewModel(data));
		}

		public ActionResult DetailOfGopY(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_GOPY,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateGopY(FormCollection forms)
		{
			string id = forms.GetValues("GopY_ID")[0];
			string tieude = forms.GetValues("GopY_TieuDe")[0];
			string noidung = forms.GetValues("GopY_NoiDung")[0];
			string email = forms.GetValues("GopY_Email")[0];
			string ngaygopy = forms.GetValues("GopY_NgayGopY")[0];
			string currentpage = forms.GetValues("GopY_CurrentPage")[0];
			var gopy = _repGopY.SelectByID(int.Parse(id));
			gopy.TieuDe = tieude;
			gopy.NoiDung = noidung;
			gopy.Email = email;
			gopy.NgayGopY = DateTime.Now;
			var result = _repGopY.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOPY,
				CurrentPage = int.Parse(currentpage)
			};
			return View("SelectGopY", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertGopY(FormCollection forms)
		{
			string tieude = forms.GetValues("GopY_TieuDe")[0];
			string noidung = forms.GetValues("GopY_NoiDung")[0];
			string email = forms.GetValues("GopY_Email")[0];
			string ngaygopy = forms.GetValues("GopY_NgayGopY")[0];
			var newItem = new GopY
			{
				TieuDe = tieude,
				NoiDung = noidung,
				Email = email,
				NgayGopY = ngaygopy == "" ? DateTime.Now : DateTime.Parse(ngaygopy)
			};
			var result = _repGopY.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOPY,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectGopY", CreateViewModel(data));
		}


	}
}